/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package scm.rawMaterialPurchasing.managedbean;

import commonInfrastructure.accountManagement.entity.SalesmanEntity;
import commonInfrastructure.accountManagement.entity.WarehousePersonnelEntity;
import commonInfrastructure.accountManagement.session.WarehousePersonnelSessionBean;
import crm.clientManagement.entity.CompanyEntity;
import java.io.IOException;
import java.io.Serializable;
import java.text.ParseException;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import javax.servlet.http.HttpServletRequest;
import scm.rawMaterialPurchasing.entity.SupplierEntity;
import scm.rawMaterialPurchasing.session.SupplierSessionBean;

/**
 *
 * @author GKnight
 */
@ManagedBean
@ViewScoped
public class ManageSupplierManagedBean implements Serializable{
    
    private Long id;
    @EJB
    SupplierSessionBean supplierSessionBean;
    @EJB
    WarehousePersonnelSessionBean warehouseSession;
    
    private SupplierEntity selectedSupplier;
    private boolean editMode;
    
    private boolean canDelete=true;
//    private FacesMessage msg;

    public ManageSupplierManagedBean() {
        selectedSupplier=new SupplierEntity();
    }
    
    public void initView(PhaseEvent event){
//        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
//        Long loginId = (Long) request.getSession().getAttribute("userId");
//        WarehousePersonnelEntity user = warehouseSession.getWarehousemanById(loginId);
//        if (user.getSalesManLevel().equals("4")){
//            canDelete = true;
//        }
//        else{
//            canDelete = false;
//        }
    }
    
    
    public boolean isEditMode() {  
        return editMode;  
    }  
  
    public void setEditMode(boolean editMode) {  
        this.editMode = editMode;  
    }  

    public List<SupplierEntity> getSuppliers() {
        return supplierSessionBean.getAllSuppliers();
    }

    public void deleteSupplier(ActionEvent event)throws ParseException {
       id=(Long)event.getComponent().getAttributes().get("param1");
       supplierSessionBean.deleteSupplierById(id);
    }
    
    public void editSupplier(SupplierEntity supplier) throws IOException{
       FacesContext.getCurrentInstance().getExternalContext().getFlash().put("supplier", supplier);
       FacesContext.getCurrentInstance().getExternalContext().redirect("editSupplier.xhtml");
    }
    
    
    /**
     * @return the canDelete
     */
    public boolean isCanDelete() {
        return canDelete;
    }

    /**
     * @param canDelete the canDelete to set
     */
    public void setCanDelete(boolean canDelete) {
        this.canDelete = canDelete;
    }

    /**
     * @return the selectedSupplier
     */
    public SupplierEntity getSelectedSupplier() {
        return selectedSupplier;
    }

    /**
     * @param selectedSupplier the selectedSupplier to set
     */
    public void setSelectedSupplier(SupplierEntity selectedSupplier) {
        this.selectedSupplier = selectedSupplier;
    }
}
